#include <iostream>
#include <unistd.h>
#include <sys/epoll.h>
#define EPOLL_CREATE_SIZE 200
#define EPOLL_WAIT_MAX 20
#define EPOLL_WAIT_TIMEOUT 1000
// 封装epoll模型
class Epoll
{
public:
    Epoll() = default;
    void Creat()
    {
        _epfd = epoll_create(EPOLL_CREATE_SIZE);
        if(_epfd < 0)
        {
            std::cout << "epoll_create fail" << std::endl;
            exit(6);
        }
    }
    void Ctl(int op, int sock, int event)
    {
        epoll_event eve;
        memset(&eve, 0, sizeof(eve));
        eve.data.fd = sock;
        eve.events = event;
        int ret = epoll_ctl(_epfd, op, sock, &eve);
        if (ret < 0)
        {
            std::cout << "epoll_ctl fail:" << strerror(errno) << errno << std::endl;
            exit(7);
        }
    }
    int Wait(epoll_event* even)
    {
        int num = epoll_wait(_epfd, even, EPOLL_WAIT_MAX, EPOLL_WAIT_TIMEOUT);
        if(num < 0)
        {
            std::cout << "epoll_wait fail" << std::endl;
            exit(8);
        }
        return num;
    }
    ~Epoll()
    {
        if(_epfd > 0)
            close(_epfd);
    }

private:
    int _epfd;
};